<?php

namespace app\admin\controller;

use app\common\service\ApiService;
use app\admin\validate\ApiValidate;
use app\admin\model\Api;
use app\common\attributes\RandyValidate;
use app\common\attributes\Menu;
use randy\tools\units\ExtendArray;
use think\annotation\Inject;
use think\response\Json;
use think\App;

class ApiController  extends AdminBaseController
{

    protected Api $model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new Api();
    }

    #[Inject]
    protected  ApiService $service ;

    #[menu('查看列表')]
    public function index(): Json
    {
         $data = $this->getParams();
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $extendQueryHelper = $this->buildparams();
        $list = $this->service->getApiList($extendQueryHelper, $data);
        foreach ($list as $row) {
        }
        return $this->success($list);
    }

    public function tree()
    {
        $list = Api::build()
                   ->select()
                   ->toArray();
        $res = ExtendArray::listToTree($list, 'api_id', 'pid', 'child');

        return $this->returnSuccess($res);
    }

    #[menu('查看列表')]
    public function list()
    {
        $data = $this->getParams();
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $extendQueryHelper = $this->buildparams();
        $extendQueryHelper->setIsPage(false);
        $list = $this->service->getApiList($extendQueryHelper, $data);
        foreach ($list as $row) {

           }
           return $this->success($list);
       }


    #[menu('查看详情')]
    #[RandyValidate(validate: ApiValidate::class, scene: 'read')]
    public function read( $api_id ): Json
    {
        $res = $this->service->getApiItem($api_id);
        return $this->returnSuccess($res);
    }

    #[menu('删除')]
    #[RandyValidate(validate: ApiValidate::class, scene: 'del')]
    public function del( $api_id ):Json
    {
        $res = $this->service->delApiItem( $api_id );
        return $this->returnSuccess($res);
    }

    #[menu('更新')]
    #[RandyValidate(validate: ApiValidate::class, scene: 'update')]
    public function update( $api_id ): \think\response\Json
    {
        $data = $this->getParams();
        $res = $this->service->setApiItem($api_id ,$data);
        return $this->returnSuccess();
    }

    #[menu('新增')]
    #[RandyValidate(validate: ApiValidate::class, scene: 'save')]
    public function save():Json
    {
        $data = $this->getParams();
        $res = $this->service->addApiItem($data);
        return $this->returnSuccess($res);
    }



}
